package com.bnlr.lrparking.appui.mine.personal;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bnlr.lrparking.app.Application;
import com.bnlr.lrparking.R;
import com.bnlr.lrparking.base.BaseActivity;
import com.bnlr.lrparking.app.AppConfig;
import com.bnlr.lrparking.constant.LrConstant;
import com.bnlr.lrparking.constant.Urls;
import com.bnlr.lrparking.model.VerifyCodeM;
import com.bnlr.lrparking.util.CountDownTimerUtils;
import com.bnlr.lrparking.util.CommonUtil;
import com.bnlr.lrparking.view.ClearEditText;
import com.bnlr.nohttp.CallServer;
import com.bnlr.nohttp.CustomHttpListener;
import com.yolanda.nohttp.NoHttp;

import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/6/6.
 * 修改密码
 */

public class ChangePwdActivity extends BaseActivity {

    @BindView(R.id.tv_nav_title)
    TextView tvNavTitle;
    @BindView(R.id.tv_nav_right)
    TextView tvNavRight;
    @BindView(R.id.iv_nav_right)
    ImageView ivNavRight;
    @BindView(R.id.iv_nav_back)
    ImageView ivNavBack;
    @BindView(R.id.et_change_phone)
    TextView etChangePhone;
    @BindView(R.id.tv_change_yzm)
    TextView tvChangeYzm;
    @BindView(R.id.et_change_yzm)
    ClearEditText etChangeYzm;
    @BindView(R.id.et_change_newpsw)
    ClearEditText etChangeNewpsw;
    @BindView(R.id.et_change_twopsw)
    ClearEditText etChangeTwopsw;
    @BindView(R.id.btn_changePwd)
    Button btnChangePwd;
    @BindView(R.id.rely_onepsd)
    RelativeLayout Relyonepsd;
    @BindView(R.id.rely_twopsd)
    RelativeLayout Relytwopsd;
    @BindView(R.id.img_onepsd)
    ImageView Imgonepsd;
    @BindView(R.id.img_twopsd)
    ImageView Imgtwopsd;
    int flag_new = 1;
    int flag_sure = 1;
    String mobile = "", password = "", twopassword = "";
    String yzm="";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_changepwd);
        ButterKnife.bind(this);
        tvNavTitle.setText("修改密码");
        mobile=AppConfig.getInstance().getString("userName", "");
        etChangePhone.setText(mobile);
        etChangeNewpsw.addTextChangedListener(textWatcher);
        etChangeTwopsw.addTextChangedListener(textWatcher);
        etChangeYzm.addTextChangedListener(textWatcher);
        tvChangeYzm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SendYzm();
            }
        });
    }


    private TextWatcher textWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void afterTextChanged(Editable editable) {
            password = etChangeNewpsw.getText().toString();
            twopassword = etChangeTwopsw.getText().toString();
            yzm=etChangeYzm.getText().toString();
            btnChangePwd.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (TextUtils.isEmpty(etChangeYzm.getText().toString())) {
                        CommonUtil.showToask(baseContext, "请输入验证码");
                        return;
                    }

                    if (TextUtils.isEmpty(password)) {
                        CommonUtil.showToask(baseContext, "密码不能为空!");
                        return;
                    } else if (CommonUtil.isContainChinese(password)) {
                        CommonUtil.showToask(baseContext, "密码格式错误!");
                        return;
                    } else if (password.length() < 6 || password.length() > 16) {
                        CommonUtil.showToask(baseContext, "密码长度6~16");
                        return;
                    }
                    if (password.equals(twopassword)) {
                        Change();
                    } else {
                        CommonUtil.showToask(baseContext, "请检查输入的密码是否一致");
                    }
                }
            });
        }
    };


    @Override
    public void doClick(View v) {
        super.doClick(v);
        switch (v.getId()) {
            case R.id.rely_onepsd:
                if (flag_new == 1) {
                    flag_new = 2;
                    Imgonepsd.setImageResource(R.mipmap.login_icon05);
                    etChangeNewpsw.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                } else {
                    flag_new = 1;
                    Imgonepsd.setImageResource(R.mipmap.login_icon04);
                    etChangeNewpsw.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
                break;
            case R.id.rely_twopsd:
                if (flag_sure == 1) {
                    flag_sure = 2;
                    Imgtwopsd.setImageResource(R.mipmap.login_icon05);
                    etChangeTwopsw.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                } else {
                    flag_sure = 1;
                    Imgtwopsd.setImageResource(R.mipmap.login_icon04);
                    etChangeTwopsw.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
                break;
        }
    }



    private void SendYzm() {
        try {
            mRequest = NoHttp.createStringRequest(Urls.IP, LrConstant.POST);
            mRequest.add("action", "sendValidateMCD");
            mRequest.add("phonePara", Application.DEVICE_ID);
            mRequest.add("phone", mobile);
            mRequest.add("flag", "1");
            mRequest.add("uid", AppConfig.getInstance().getString("uid", ""));
            // 添加到请求队列
            CallServer.getRequestInstance().add(baseContext, mRequest,
                    new CustomHttpListener<VerifyCodeM>(baseContext, true, VerifyCodeM.class) {
                        @Override
                        public void doWork(VerifyCodeM data, String code) {

                        }

                        @Override
                        public void onFinally(JSONObject obj, String code, boolean isSucceed) {

                            try {
                                super.onFinally(obj, code, isSucceed);
                                if ("1".equals(code)) {
                                    setVerifica(tvChangeYzm);
                                } else {
                                    CommonUtil.showToask(baseContext, obj.getString("msg"));
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }, true);
        } catch (Exception e) {

        }
    }

    private void Change() {
        try {
            mRequest = NoHttp.createStringRequest(Urls.IP, LrConstant.POST);
            mRequest.add("action", "UpdatePwdByUserId");
            mRequest.add("phonePara", Application.DEVICE_ID);
            mRequest.add("uid", AppConfig.getInstance().getString("uid", ""));
            mRequest.add("pwd", password);
            mRequest.add("code",yzm);
            // 添加到请求队列
            CallServer.getRequestInstance().add(baseContext, mRequest,
                    new CustomHttpListener<JSONObject>(baseContext, false, null) {
                        @Override
                        public void doWork(JSONObject data, String code) {
                        }

                        @Override
                        public void onFinally(JSONObject obj, String code, boolean isSucceed) {
                            try {
                                super.onFinally(obj, code, isSucceed);
                                if ("1".equals(code)) {
                                    CommonUtil.showToask(baseContext, "修改成功");
                                    ChangePwdActivity.this.finish();
                                } else {
                                    CommonUtil.showToask(baseContext, obj.getString("msg"));
                                }

                            } catch (Exception e) {
                                e.printStackTrace();

                            }
                        }
                    }, true);
        } catch (Exception e) {
        }
    }

    private void setVerifica(TextView verifica) {
        CountDownTimerUtils mCountDownTimerUtils = new CountDownTimerUtils(verifica, 60000, 1000);
        mCountDownTimerUtils.start();
    }
}
